﻿namespace JelloPhysics
{
    using Microsoft.Xna.Framework;
    using System;

    public class PressureBody : SpringBody
    {
        private float[] mEdgeLengthList;
        private float mGasAmount;
        private Vector2[] mNormalList;
        private float mVolume;

        public PressureBody(World w, ClosedShape s, float massPerPoint, float gasPressure, float shapeSpringK, float shapeSpringDamp, float edgeSpringK, float edgeSpringDamp, Vector2 pos, float angleInRadians, Vector2 scale, bool kinematic) : base(w, s, massPerPoint, shapeSpringK, shapeSpringDamp, edgeSpringK, edgeSpringDamp, pos, angleInRadians, scale, kinematic)
        {
            this.mGasAmount = gasPressure;
            this.mNormalList = new Vector2[base.mPointMasses.Count];
            this.mEdgeLengthList = new float[base.mPointMasses.Count];
        }

        public override void accumulateInternalForces()
        {
            base.accumulateInternalForces();
            this.mVolume = 0f;
            for (int i = 0; i < base.mPointMasses.Count; i++)
            {
                int num2 = (i > 0) ? (i - 1) : (base.mPointMasses.Count - 1);
                int num3 = (i < (base.mPointMasses.Count - 1)) ? (i + 1) : 0;
                Vector2 v = new Vector2();
                v.X = base.mPointMasses[i].Position.X - base.mPointMasses[num2].Position.X;
                v.Y = base.mPointMasses[i].Position.Y - base.mPointMasses[num2].Position.Y;
                VectorTools.makePerpendicular(ref v);
                Vector2 vector2 = new Vector2();
                vector2.X = base.mPointMasses[num3].Position.X - base.mPointMasses[i].Position.X;
                vector2.Y = base.mPointMasses[num3].Position.Y - base.mPointMasses[i].Position.Y;
                VectorTools.makePerpendicular(ref vector2);
                Vector2 vector3 = new Vector2();
                vector3.X = v.X + vector2.X;
                vector3.Y = v.Y + vector2.Y;
                float num4 = (float) Math.Sqrt((double) ((vector3.X * vector3.X) + (vector3.Y * vector3.Y)));
                if (num4 > 0.001f)
                {
                    vector3.X /= num4;
                    vector3.Y /= num4;
                }
                float num5 = (float) Math.Sqrt((double) ((vector2.X * vector2.X) + (vector2.Y * vector2.Y)));
                this.mNormalList[i] = vector3;
                this.mEdgeLengthList[i] = num5;
                float num7 = (Math.Abs((float) (base.mPointMasses[i].Position.X - base.mPointMasses[num3].Position.X)) * Math.Abs(vector3.X)) * num5;
                this.mVolume += 0.5f * num7;
            }
            float num8 = 1f / this.mVolume;
            for (int j = 0; j < base.mPointMasses.Count; j++)
            {
                int index = (j < (base.mPointMasses.Count - 1)) ? (j + 1) : 0;
                float num11 = (num8 * this.mEdgeLengthList[j]) * this.mGasAmount;
                base.mPointMasses[j].Force.X += this.mNormalList[j].X * num11;
                base.mPointMasses[j].Force.Y += this.mNormalList[j].Y * num11;
                base.mPointMasses[index].Force.X += this.mNormalList[index].X * num11;
                base.mPointMasses[index].Force.Y += this.mNormalList[index].Y * num11;
            }
        }

        public float GasPressure
        {
            get
            {
                return this.mGasAmount;
            }
            set
            {
                this.mGasAmount = value;
            }
        }

        public float Volume
        {
            get
            {
                return this.mVolume;
            }
        }
    }
}

